package www.rlicker.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import www.rlicker.model.Banner;
import www.rlicker.model.GroupResource;
import www.rlicker.model.SysRole;
import www.rlicker.mybatis.Criteria;
import www.rlicker.mybatis.PageResult;
import www.rlicker.service.IBannerService;
import www.rlicker.service.IGroupResourceService;
import www.rlicker.service.ISysRoleService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.JsonResult;
import www.rlicker.utils.StringUtil;

import com.alibaba.fastjson.JSONArray;

@Controller
@RequestMapping(value="banner")
public class BannerController extends BaseController{
	
	@Autowired
	private IBannerService service;
	
	@Autowired
	private ISysRoleService sysRoleService;
	
	@Autowired
	private IGroupResourceService groupResourceService;
	
	@RequestMapping("/showlist")
	public String showlist(ModelMap map, HttpServletRequest request){
		SysRole sysRole = sysRoleService.queryByRoleKey(getUserId(request));
		if (sysRole != null) {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("resourceIdList", getResourceId(sysRole.getResourceId()));
			params.addParam("parent_resource_id",new Long(request.getParameter("id")));
			params.addParam("status", 0);
			params.addParam("showType", 1);
			List<GroupResource> lists = groupResourceService.queryPageList(params);
            map.put("gpLists", JSONArray.toJSONString(lists));
		}
        map.put("id", request.getParameter("id"));
		return "banner/bannerList";
	}
	
	@RequestMapping("/querypage")
	@ResponseBody
	public PageResult queryPage(HttpServletRequest request){
		 Criteria<Banner> param = new Criteria<Banner>();
		 if (!StringUtil.isNullOrBlank(request.getParameter("type"))) {
			 param.addParam("type", request.getParameter("type"));
		 }
	     if (!StringUtil.isNullOrBlank(request.getParameter("pageNumber"))) {
	    	 param.addParam("startIndex", Integer.parseInt(request.getParameter("pageNumber")));
	     }
	     if (!StringUtil.isNullOrBlank(request.getParameter("pageSize"))) {
	    	 param.addParam("endPage", Integer.parseInt(request.getParameter("pageSize")));
	     }
	     List<Map<String,Object>> list = service.queryPage(param);
	     PageResult pageResult = new PageResult();
	     pageResult.setTotal(service.queryPageCount(param));
		 pageResult.setRows(list);
		 return pageResult;	
	}
	
	@RequestMapping("/dispay")
	public String dispay(String img,String resourceId,ModelMap map){
		map.put("img", img);
		map.put("resourceId", resourceId);
		return "banner/bannerDisbay";
	}
	
	@RequestMapping("/detail")
	public String detail(String id,String resourceId,ModelMap map){
		if (!StringUtil.isNullOrBlank(id)) {
			Banner banner = service.queryEntity(new Long(id));
			map.put("banner", banner);
		}
		map.put("resourceId", resourceId);
		return "banner/bannerDetail";
	}

	@ResponseBody
	@RequestMapping("/delete")
	public JsonResult delete(String id){
		String ret = null;
		String[] split = id.split(",");
		for (int i = 0; i < split.length; i++) {
			ret = service.deleteById(new Long(split[i]));
			String content ="删除了"+split[i]+"banner配置";
			insertLog(content);
		}
		JsonResult jr = JsonResult.newInstance(); 
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
	
	@ResponseBody
	@RequestMapping(value="/insert" , method = { RequestMethod.POST })
	public JsonResult insert(HttpServletRequest request){
	  JsonResult jr = JsonResult.newInstance(); 
	  Banner banner = new Banner();
	  //banner.setId(new Long(getSeqNum()));
	  banner.setType(Integer.parseInt(request.getParameter("type")));
	  banner.setPictureTitie(request.getParameter("pictureTitie"));
	  banner.setContent(request.getParameter("content"));
	  banner.setUpdateTime(new Date());
	  String ret = service.insert(banner);
	  String content ="添加了banner配置";
	  insertLog(content);
	  return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
	
	@ResponseBody
	@RequestMapping(value="/update" , method = { RequestMethod.POST })
	public JsonResult update(HttpServletRequest request){
		JsonResult jr = JsonResult.newInstance(); 
		Criteria<Banner> param = new Criteria<Banner>();
		Banner banner = new Banner();
		banner.setPictureTitie(request.getParameter("pictureTitie"));
		banner.setContent(request.getParameter("content"));
		banner.setUpdateTime(new Date());
		param.setRecord(banner);
		param.addParam("id", request.getParameter("id"));
	    String ret = service.update(param);
		String content ="修改了"+request.getParameter("id")+"banner配置";
		insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
	
}
